package com.epfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.epfresh.R;
import com.epfresh.adapter.AreaStoreViewBinder;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.RequestEntityMap;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.api.widget.ptr.PtrDefaultHandler;
import com.epfresh.api.widget.ptr.PtrFrameLayout;
import com.epfresh.api.widget.ptr.PtrHandler;
import com.epfresh.bean.MerchantEntity;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.loadmore.LoadMoreDelegate;
import multitype.loadmore.LoadMoreItem;
import multitype.loadmore.LoadMoreViewBinder;

public class MoreAreaStoreActivity extends BaseActivity {
    private MultiTypeAdapter multiAdapter;
    private List<Object> items = new ArrayList<>();
    private PtrFrameLayout ptrFrameLayout;
    private LoadMoreDelegate loadMoreDelegate = new LoadMoreDelegate();

    private RecyclerView rv_store_list;
    private String marketId, areaId;
    private int pageNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_area_store);

        getIntentData();
        initView();
        refresh();
    }

    private void getIntentData() {
        marketId = getIntent().getStringExtra("marketId");
        areaId = getIntent().getStringExtra("areaId");
    }

    private void initView() {
        toolbarTitle.setText("店铺");
        rv_store_list = findViewById(R.id.rv_store_list);
        ptrFrameLayout = findViewById(R.id.ptr_frame_layout);
        ptrFrameLayout.setPtrHandler(ptrHandler);
        loadMoreDelegate.attach(rv_store_list, loadMoreSubject);

        multiAdapter = new MultiTypeAdapter();
        AreaStoreViewBinder areaStoreViewBinder = new AreaStoreViewBinder();
        areaStoreViewBinder.setMoreStoreClickListener(new AreaStoreViewBinder.OnMoreStoreClickListener() {
            @Override
            public void onMoreStoreClick(String areaId) {

            }

            @Override
            public void onItemClick(MerchantEntity.AreaBean.StoresBean item) {
                Intent storeIntent = new Intent(MoreAreaStoreActivity.this, CommonStoreActivity.class);
                storeIntent.putExtra("storeId",item.getId());
                startActivity(storeIntent);
            }
        });
        LoadMoreViewBinder loadMoreViewBinder = loadMoreDelegate.getLoadMoreViewBinder();

        multiAdapter.register(MerchantEntity.AreaBean.StoresBean.class, areaStoreViewBinder);
        multiAdapter.register(LoadMoreItem.class, loadMoreViewBinder);

        multiAdapter.setItems(items);
        rv_store_list.setLayoutManager(new LinearLayoutManager(this));
        rv_store_list.setAdapter(multiAdapter);
    }

    private PtrHandler ptrHandler = new PtrHandler() {
        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            return PtrDefaultHandler.checkContentCanBePulledDown(frame, rv_store_list, header);
        }

        @Override
        public void onRefreshBegin(PtrFrameLayout frame) {
            refresh();
        }
    };

    private LoadMoreDelegate.LoadMoreSubject loadMoreSubject = new LoadMoreDelegate.LoadMoreSubject() {
        @Override
        public boolean canLoad() {
            return !ptrFrameLayout.isRefreshing();
        }

        @Override
        public void onLoadMore() {
            loadMoreDelegate.setStatusLoading();
            loadMore();
        }
    };

    private void refresh() {
        items.clear();
        pageNum = 0;
        reqStoreList();
    }

    private void loadMore() {
        pageNum += 1;
        reqStoreList();
    }

    private void reqStoreList() {
        RequestEntityMap requestEntity = new RequestEntityMap();
        requestEntity.setCmd(AppPurchaseRouterConstant.cmd_merchant);
        requestEntity.putParameter("id", marketId);
        requestEntity.putParameter("marketAreaId", areaId);
        requestEntity.putParameter("pageNumber",pageNum);
        requestEntity.putParameter("pageSize",10);

        request(requestEntity, AppPurchaseRouterConstant.cmd_merchant, new OnRequestListener<MerchantEntity>() {
            @Override
            public MerchantEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr,MerchantEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                T.toast(failCode + "");
                ptrFrameLayout.refreshComplete();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                T.toast(failCode + "");
                ptrFrameLayout.refreshComplete();
            }

            @Override
            public void onStart(Object tag) {
            }

            @Override
            public void onResponse(ResponseEntity<MerchantEntity> response, Object tag) {
                ptrFrameLayout.refreshComplete();

                if (response.getResponseElement().getMarketAreas() != null && response.getResponseElement().getMarketAreas().size() > 0) {
                    List<MerchantEntity.AreaBean> marketAreas = response.getResponseElement().getMarketAreas();
                    if (marketAreas.get(0).getStores() != null && marketAreas.get(0).getStores().size() > 0) {
                        List<MerchantEntity.AreaBean.StoresBean> stores = new ArrayList<>(marketAreas.get(0).getStores());
                        loadMoreDelegate.addAllItem(items,stores);

                        if (!marketAreas.get(0).isMore()) {
                            loadMoreDelegate.setStatusNoMore();
                        } else {
                            loadMoreDelegate.setStatusNormal();
                        }
                    }
                }
                multiAdapter.notifyDataSetChanged();
            }
        });
    }
}
